package com.itboye.eweilisten.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.app.MyApplication;
import com.itboye.eweilisten.model.ZhangHuBean;
import com.itboye.eweilisten.util.sputil.Const;
import com.itboye.eweilisten.util.widget.TimeFormat;
import com.itboye.eweilisten.util.widget.XImageLoader;

import java.text.DecimalFormat;
import java.util.List;

/**
 * 我的订单
 */
public class OrderAdapter extends BaseAdapter implements View.OnClickListener {

    List<ZhangHuBean.InfoList> bean;
    Context context;
    AdapterOrderFace orderFace;

    public OrderAdapter(Context context, List<ZhangHuBean.InfoList> bean, AdapterOrderFace orderFace) {
        this.bean = bean;
        this.context = context;
        this.orderFace = orderFace;
    }

    @Override
    public int getCount() {
        return bean.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHouder houder = null;
        if (convertView == null) {
            houder = new ViewHouder();
            convertView = LayoutInflater.from(MyApplication.instance).inflate(R.layout.zhanghu_adapter, null);
            houder.tvOrderTime = (TextView) convertView.findViewById(R.id.tvOrderTime);
            houder.tvOrderStu = (TextView) convertView.findViewById(R.id.tvOrderStu);
            houder.zhuanghu_title = (TextView) convertView.findViewById(R.id.zhuanghu_title);
            houder.zhuanghu_money = (TextView) convertView.findViewById(R.id.zhuanghu_money);
            houder.zhuanghu_yue = (TextView) convertView.findViewById(R.id.zhuanghu_yue);
            houder.zhuanghu_num = (TextView) convertView.findViewById(R.id.zhuanghu_num);
            houder.zhuanghu_jianshu = (TextView) convertView.findViewById(R.id.zhuanghu_jianshu);
            houder.zhuanghu_zong = (TextView) convertView.findViewById(R.id.zhuanghu_zong);
            houder.zhuanghu_quxiao = (TextView) convertView.findViewById(R.id.zhuanghu_quxiao);
            houder.zhuanghu_zhifu = (TextView) convertView.findViewById(R.id.zhuanghu_zhifu);
            houder.zhuanghu_Ok = (TextView) convertView.findViewById(R.id.zhuanghu_Ok);
            houder.zhuanghu_Cler = (TextView) convertView.findViewById(R.id.zhuanghu_Cler);
            houder.zhuanghu_img = (ImageView) convertView.findViewById(R.id.zhuanghu_img);
            houder.relatMorenOrder = (RelativeLayout) convertView.findViewById(R.id.relatMorenOrder);
            houder.relatQueOrder = (RelativeLayout) convertView.findViewById(R.id.relatQueOrder);
            houder.relatDeleteOrder = (RelativeLayout) convertView.findViewById(R.id.relatDeleteOrder);
            houder.tip=(TextView)convertView.findViewById(R.id.tv_tip);
            convertView.setTag(houder);
        } else {
            houder = (ViewHouder) convertView.getTag();
        }
        onText(bean, position, houder);
        onClik(houder, position);
        return convertView;
    }

    public void onClik(ViewHouder houder, int position) {
        houder.zhuanghu_quxiao.setOnClickListener(this);//取消订单
        houder.zhuanghu_zhifu.setOnClickListener(this);//立即支付
        houder.zhuanghu_Cler.setOnClickListener(this);//删除订单
        houder.zhuanghu_Ok.setOnClickListener(this);//确认收货
        houder.zhuanghu_quxiao.setTag(position);//取消订单
        houder.zhuanghu_zhifu.setTag(position);//立即支付
        houder.zhuanghu_Cler.setTag(position);//删除订单
        houder.zhuanghu_Ok.setTag(position);//确认收货
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.zhuanghu_quxiao:
                orderFace.onDelete(v);
                break;
            case R.id.zhuanghu_zhifu:
                orderFace.onPliay(v);
                break;
            case R.id.zhuanghu_Cler:
                orderFace.onCanler(v);
                break;
            case R.id.zhuanghu_Ok:
                orderFace.onConfirm(v);
                break;
        }
    }

    private class ViewHouder {
        private TextView tvOrderTime;//时间
        private TextView tvOrderStu;//是否付款
        private TextView zhuanghu_title;//标题
        private TextView zhuanghu_money;//支付金额
        private TextView zhuanghu_yue;//月份
        private TextView zhuanghu_num;//数量
        private TextView zhuanghu_jianshu;//共计
        private TextView zhuanghu_zong;//实际支付
        private TextView zhuanghu_quxiao;//取消订单
        private TextView zhuanghu_zhifu;//立即支付
        private TextView zhuanghu_Cler;//删除订单
        private TextView zhuanghu_Ok;//确认收货
        private ImageView zhuanghu_img;
        private RelativeLayout relatMorenOrder;//默认
        private RelativeLayout relatQueOrder;//确认订单
        private RelativeLayout relatDeleteOrder;//删除订单
        private TextView tip;//温馨提示
    }

    public void onText(List<ZhangHuBean.InfoList> bean, int position, ViewHouder houder) {
        houder.tvOrderTime.setText("订单时间:" + TimeFormat.DateFormat(bean.get(position).getCreatetime()));

        for (int i = 0; i < bean.get(position).getItems().size(); i++) {
            DecimalFormat df = new DecimalFormat("######0.00");
            double OriPrice = Double.parseDouble(bean.get(position).getItems().get(i).getOri_price());
            double Price = Double.parseDouble(bean.get(position).getItems().get(i).getPrice());
            double PriceAll=Double.parseDouble(bean.get(position).getPrice());
            String priceType = df.format(Price / 100);
            String pirceType1=df.format(PriceAll/100);
            houder.zhuanghu_title.setText(bean.get(position).getItems().get(i).getName());
          //  houder.zhuanghu_money.setText(OriPrice / 100 + "");
            houder.zhuanghu_yue.setText(bean.get(position).getItems().get(i).getSku_desc());
            houder.zhuanghu_num.setText("X" + bean.get(position).getItems().get(i).getCount());
            houder.zhuanghu_jianshu.setText("共计" + bean.get(position).getItems().get(i).getCount() + "件商品");
            houder.zhuanghu_money.setText("¥" + priceType);
            houder.zhuanghu_zong.setText("¥" + pirceType1);
            XImageLoader.load(Const.IMAGE_HEAD + bean.get(position).getItems().get(i).getImg(), houder.zhuanghu_img);
        }
        String status = bean.get(position).getQuery_status();

        if (status.equals("0")) {

        } else if (status.equals("1")) {
            houder.tvOrderStu.setText("待付款");
            houder.relatMorenOrder.setVisibility(View.VISIBLE);//默认
            houder.relatQueOrder.setVisibility(View.GONE);//确认订单
            houder.tip.setVisibility(View.VISIBLE);//代付款项有温馨提示不退不换
            //     houder.relatDeleteOrder.setVisibility(View.GONE);//删除订单
        } else if (status.equals("2")) {
            houder.tvOrderStu.setText("已付款");
            houder.relatMorenOrder.setVisibility(View.GONE);//默认
            houder.relatQueOrder.setVisibility(View.GONE);//确认订单
            houder.tip.setVisibility(View.GONE);
            //   houder.relatDeleteOrder.setVisibility(View.GONE);//删除订单
        } else if (status.equals("3")) {
            houder.tvOrderStu.setText("已发货");
            houder.relatMorenOrder.setVisibility(View.GONE);//默认
            houder.relatQueOrder.setVisibility(View.VISIBLE);//确认订单
            houder.tip.setVisibility(View.GONE);
            //   houder.relatDeleteOrder.setVisibility(View.GONE);//删除订单
        } else if (status.equals("7")) {
            houder.tvOrderStu.setText("交易成功");
            houder.relatMorenOrder.setVisibility(View.GONE);//默认
            houder.relatQueOrder.setVisibility(View.GONE);//确认订单
            houder.tip.setVisibility(View.GONE);
            //    houder.relatDeleteOrder.setVisibility(View.GONE);//删除订单
        } else if (status.equals("8")) {
            houder.tvOrderStu.setText("交易关闭");
            houder.relatMorenOrder.setVisibility(View.GONE);//默认
            houder.relatQueOrder.setVisibility(View.GONE);//确认订单
            houder.tip.setVisibility(View.GONE);
            //    houder.relatDeleteOrder.setVisibility(View.VISIBLE);//删除订单
        }


    }

    public interface AdapterOrderFace {
        void onConfirm(View view);//确认收货

        void onCanler(View view);//取消订单

        void onDelete(View view);//删除

        void onPliay(View view);//立即支付
    }
}
